home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 118_01.zip / RAT2C.BDS < prev    next >
Text File  |  1993-06-03  |  4KB  |  229 lines

  1. /* BDS C program to convert from RATFOR to C style comments
  2.  * source:  rat2c.bds
  3.  * version: May 21, 1981.
  4.  */
  5.  
  6. /* define globals */
  7.  
  8. #include bdscio.h
  9.  
  10. #define YES 1
  11. #define NO  0
  12. #define EOS 0
  13. #define CR  13
  14. #define LF  10
  15.  
  16. #define VERSION "ver: May 21, 1981"
  17. #define SIGNON    "Welcome to RAT4 to C comment converter.  "
  18.  
  19.  
  20. int  input;
  21. int  output;
  22.  
  23. char buffer[100];        /* general buffer */
  24. char inbuf[BUFSIZ];        /* input file buffer */
  25. char outbuf[BUFSIZ];        /* output file buffer */
  26.  
  27. /* convert one file after another interactively */
  28.  
  29. main()
  30. {
  31.     /* sign on */
  32.     puts(SIGNON);
  33.     puts(VERSION);
  34.     ccrlf();
  35.     /* mark files as closed */
  36.     input = -1;
  37.     output = -1;
  38.     while (1) {
  39.         if (openio() == NO) {
  40.             closeio();
  41.             break;
  42.         }
  43.         convert();
  44.         closeio();
  45.     }
  46.     puts("End of comment converter");
  47.     ccrlf();
  48. }
  49.  
  50. /* open an input and output file.
  51.  * the input file must exist.
  52.  * the output file must not exist.
  53.  * return NO if the user gives up by entering a null file name.
  54.  */
  55.  
  56. openio()
  57. {
  58.     if (openin() == NO) {
  59.         return(NO);
  60.     }
  61.     else {
  62.         return(openout());
  63.     }
  64. }
  65.  
  66. /* open the input file, which must exist.
  67.  * keep trying until the file is open or the user gives up.
  68.  */
  69.  
  70. openin()
  71. {
  72.     while (1) {
  73.         ccrlf();
  74.         puts("input file ?  ");
  75.         gets(buffer);
  76.         if (buffer[0] == EOS) {
  77.             return(NO);
  78.         }
  79.         input = fopen (buffer,inbuf);
  80.         if (input == -1) {
  81.             ccrlf();
  82.             puts("file not found");
  83.         }
  84.         else {
  85.             return(YES);
  86.         }
  87.     }
  88. }
  89.  
  90. /* open the output file, which must not exist.
  91.  * keep trying until the file is open or the user gives up.
  92.  */
  93.  
  94. openout()
  95. {
  96.     while (1) {
  97.         ccrlf();
  98.         puts("Output file ?  ");
  99.         gets(buffer);
  100.         if (buffer[0] == EOS) {
  101.             return(NO);
  102.         }
  103.         /* make sure the file does not exist */
  104.         output = fopen(buffer,outbuf);
  105.         if (output != -1) {
  106.             ccrlf();
  107.             puts("File exists");
  108.             fclose(outbuf);
  109.             continue;
  110.         }
  111.         output = fcreat(buffer,outbuf);        
  112.         if (output == -1) {
  113.             ccrlf();
  114.             puts("disk error");
  115.         }
  116.         else {
  117.             return(YES);
  118.         }
  119.     }
  120. }
  121.  
  122. /* close any open files */
  123.  
  124. closeio()
  125. {
  126.     if (input != -1) {
  127.         fclose(inbuf);
  128.     }
  129.     if (output != -1) {
  130.         fflush(outbuf);
  131.         fclose(outbuf);
  132.     }
  133. }
  134.  
  135. /* copy input file to output file.
  136.  * convert everything following a '#' on a line into
  137.  * a C style comment.
  138.  */
  139.  
  140. convert()
  141. {
  142.     /* copy one line at a time */
  143.     while (do1line() == NO) {
  144.         ;
  145.     }
  146. }
  147.  
  148. /* copy one line from input file to output file.
  149.  * return YES if end of file has been seen.
  150.  */
  151.  
  152. do1line()
  153. {
  154. int c;            /* the current character */
  155. int comflag;        /* YES means in comment */
  156.  
  157.     /* start off each line not in a comment */
  158.     comflag=NO;
  159.     while (1) {
  160.         /* get next character from input */
  161.         c = getc(inbuf);
  162.         if ( (c == -1) || (c == 0x1a) ) {
  163.             /* end of file */
  164.             if (comflag == YES) {
  165.                 /* finish comment */
  166.                 putc(' ',outbuf);
  167.                 putc('*',outbuf);
  168.                 putc('/',outbuf);
  169.             }
  170.             /* output end of file mark */
  171.             putc(0x01a,outbuf);
  172.             return(YES);
  173.         }
  174.         else if ((comflag == NO) &
  175.              (c == '#') ) {
  176.             /* start of comment */
  177.             comflag = YES;
  178.             putc('/',outbuf);
  179.             putc('*',outbuf);
  180.             putc(' ',outbuf);
  181.         }
  182.         else if (c == CR) {
  183.             /* end line. assume LF will follow */
  184.             c = getc(inbuf);
  185.             if (comflag == YES) {
  186.                 /* finish comment */
  187.                 putc(' ',outbuf);
  188.                 putc('*',outbuf);
  189.                 putc('/',outbuf);
  190.             }
  191.             /* finish line */
  192.             putc(CR,outbuf);
  193.             putc(LF,outbuf);
  194.             /* no EOF seen yet */
  195.             return(NO);
  196.         }
  197.         else {
  198.             /* just copy next character */
  199.             putc(c,outbuf);
  200.         }
  201.     }
  202. }
  203.  
  204. /* output CR and LF to console */        
  205.  
  206. ccrlf()
  207. {
  208.     putchar(CR);
  209.     putchar(LF);
  210. }
  211. n a comment */
  212.     comflag=NO;if (c == CR) {
  213.             /* end line. assume LF will follow */
  214.             c = getc(inbuf);
  215.             if (comflag == YES) {
  216.                 /* finish comment */
  217.                 putc(' ',outbuf);
  218.                 putc('*',outbuf);
  219.                 putc('/',outbuf);
  220.             }
  221.             /* finish line */
  222.             putc(CR,outbuf);
  223.             putc(LF,outbuf);
  224.             /* no EOF seen yet */
  225.             return(NO);
  226.         }
  227.         else {
  228.             /* just copy next character */
  229.             putc(c,outbuf);